package com.ruoyi.web.controller.tool;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.deviceControl.domain.DeviceControl;
import com.ruoyi.deviceControl.dto.LinkageControlDto;
import com.ruoyi.deviceControl.service.IDeviceControlService;
import com.ruoyi.deviceControl.service.ILinkageControlService;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.domain.dto.GradedAlarmDto;
import com.ruoyi.system.mapper.SysOperLogMapper;
import org.checkerframework.checker.units.qual.A;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider;
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.*;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static com.ruoyi.web.controller.tool.Demo.createClientNewEndpoint;

@RestController
@RequestMapping("/control")
public class ControlController {
    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    @Autowired
    private ILinkageControlService linkageControlService;

    @Autowired
    private IDeviceControlService deviceControlService;

    private final static String endPointUrl = "opc.tcp://127.0.0.1:49320";
    private final static String beltPointUrl = "opc.tcp://127.0.0.1:49320";//皮带
    private final static String otherPointUrl = "opc.tcp://127.0.0.1:49320";//其他
    private final static String monkeyCarPointUrl = "opc.tcp://127.0.0.1:49320";//猴车
    //private final static String endPointUrl = "opc.tcp://192.168.5.153:45321";
//    private final  static String endPointUrlBelt = "opc.tcp://192.168.5.60:4862";
    /**
     * 远程操控
     *
     * @throws Exception
     */
    @GetMapping("/remoteControl/{control}")
    private  void remoteControl(@PathVariable("control") String control) throws Exception {
        String[] split = control.split(",");
        if ("运输系统".equals(split[0])){
            beltControl(control);
        }else {
            assistControl(control);
        }
    }

    /**
     * 写入节点数据
     *
     * @param
     * @throws Exception
     */
    private  void writeNodeValue(int index, String passage, Boolean i) throws Exception {
        OpcUaClient client =createClientNewEndpoint(endPointUrl, "ua", "123456");
        //节点          煤矿1.皮带*.tag_name              煤矿.*.*     煤矿2.*.*
        NodeId nodeId = new NodeId(index, passage);
        i = true;
        //创建数据对象,此处的数据对象一定要定义类型，不然会出现类型错误，导致无法写入
        DataValue nowValue = new DataValue(new Variant(i), null, null);
        //写入节点数据
        StatusCode statusCode = client.writeValue(nodeId, nowValue).join();
        System.out.println("结果：" + statusCode.isGood());
    }

    /**
     * 写入节点数据 lqx
     *
     * @param
     * @throws Exception
     */
//    private void writeNodeValueByLinkageControl(int index, String passage, Boolean i) throws Exception {
    private void writeNodeValueByLinkageControl(GradedAlarmDto gradedAlarmDto) throws Exception {
        String pointUrl = null;
        List<LinkageControlDto> linkageControlDtos = linkageControlService.selectLinkageControlListByAlarmId(gradedAlarmDto.getGradedAlarmId());
        if (linkageControlDtos != null){
            for (LinkageControlDto linkageControlDto : linkageControlDtos) {
                DeviceControl deviceControl = deviceControlService.selectDeviceControlByDeviceControlId(linkageControlDto.getDeviceId());
                switch (deviceControl.getDeviceType()){
                    case "0":
                        pointUrl = beltPointUrl;
                        break;
                    case "1":
                        pointUrl = monkeyCarPointUrl;
                        break;
                    case "100":
                        pointUrl = otherPointUrl;
                        break;
                    default:
                        break;
                }
                OpcUaClient client =createClientNewEndpoint(pointUrl, "ua", "123456");
                //节点          煤矿1.皮带*.tag_name              煤矿.*.*     煤矿2.*.*
                NodeId nodeId = new NodeId(2, deviceControl.getTagName());
                //创建数据对象,此处的数据对象一定要定义类型，不然会出现类型错误，导致无法写入
                DataValue nowValue = new DataValue(new Variant(deviceControl.getTagValue()), null, null);
                //写入节点数据
                StatusCode statusCode = client.writeValue(nodeId, nowValue).join();
                System.out.println("结果：" + statusCode.isGood());
                // 延迟执行指令
                try {
                    Thread.sleep(Integer.parseInt(linkageControlDto.getDelayTime()) * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private  void beltControl(String control) throws Exception {
        String[] split = control.split(",");
        if ("1-1主井皮带".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(1,"t|NO1_40501B0",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(1,"t|NO1_40501B0",false);
                    insertOperLog(control);
                    break;
                case "急停":
                    writeNodeValue(1,"t|NO1_40501B2",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(1,"t|NO1_40501B1",true);
                    insertOperLog(control);
                    break;
            }
        }else if ("2-1副井皮带".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(1,"t|NO2_40501B0",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(1,"t|NO2_40501B0",false);
                    insertOperLog(control);
                    break;
                case "急停":
                    writeNodeValue(1,"t|NO2_40501B2",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(1,"t|NO2_40501B1",true);
                    insertOperLog(control);
                    break;
            }
        }
        switch (split[2]){
            case "总急停":
                writeNodeValue(1,"",true);
                insertOperLog(control);
                break;
            case "总复位":
                writeNodeValue(1,"",true);
                insertOperLog(control);
                break;
        }
    }

    private void insertOperLog(String control) throws UnknownHostException {
        String[] split = control.split(",");
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setOperIp(InetAddress.getLocalHost().getHostAddress());  // 操作IP
        sysOperLog.setBusinessType(10);   // 操作 类型
        sysOperLog.setTitle(split[0]);   // 功能
        sysOperLog.setOperTime(new Date());  // 操作时间
        sysOperLog.setMethod(split[1]);  // 设备
        sysOperLog.setRequestMethod(split[2]);
        sysOperLog.setOperName(SecurityUtils.getUsername());  // 操作用户
        sysOperLogMapper.insertOperlog(sysOperLog);
    }


    private  void assistControl(String control) throws Exception {
        String[] split = control.split(",");
        switch (split[0]){
            case "通风系统":
                tunnellingControl(control);
                break;
            case "压风系统":
                compressedAirControl(control);
                break;
            case "局扇系统":
                fanControl(control);
                break;
            case "供排水系统":
                waterControl(control);
                break;
            case "瓦斯系统":
                gasControl(control);
                break;
        }
    }

    private void gasControl(String control) throws Exception {
        String[] split = control.split(",");
        if ("冷却阀1".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFYKF_1",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFGF_1",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFFW_1",true);
                    insertOperLog(control);
                    break;
            }
        }else if("冷却阀2".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFYKF_2",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFGF_2",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFFW_2",true);
                    insertOperLog(control);
                    break;
            }
        }else if("冷却阀3".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFYKF_3",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFGF_3",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFFW_3",true);
                    insertOperLog(control);
                    break;
            }
        }else if("冷却阀4".equals(split[1])){
            switch (split[2]){
                case "启动":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFYKF_4",true);
                    insertOperLog(control);
                    break;
                case "停止":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFGF_4",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.瓦斯抽放泵房.BLQFFW_4",true);
                    insertOperLog(control);
                    break;
            }
        }
    }

    private void waterControl(String control) throws Exception {
        String[] split = control.split(",");
        if ("1#水泵".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYK_1",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYG_1",true);
                    insertOperLog(control);
                    break;
                case "故障复位":
                    writeNodeValue(1,"煤矿.中央水泵房.BFW_1",true);
                    insertOperLog(control);
                    break;
            }
        }else if("2#水泵".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYK_2",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYG_2",true);
                    insertOperLog(control);
                    break;
                case "故障复位":
                    writeNodeValue(1,"煤矿.中央水泵房.BFW_2",true);
                    insertOperLog(control);
                    break;
            }
        }else if("3#水泵".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYK_3",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(1,"煤矿.中央水泵房.BDJYG_3",true);
                    insertOperLog(control);
                    break;
                case "故障复位":
                    writeNodeValue(1,"煤矿.中央水泵房.BFW_3",true);
                    insertOperLog(control);
                    break;
            }
        }
    }

    private void fanControl(String control) throws Exception {
        String[] split = control.split(",");
        switch (split[1]){
            case "12水平406机巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平406机巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平406机巷局扇1.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "15水平11403机巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "11水平11402回风巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11402回风巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "11水平11401回风巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "11水平11401回风巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.11水平11401回风巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平10603机巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平10603机巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平10603机巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平401机巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平401机巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平401机巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平606机巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "12水平606机巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.12水平606机巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
            case "15水平10605底抽巷局扇主机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.ZJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.ZJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.ZJFW",true);
                    insertOperLog(control);
                }
                break;
            case "15水平10605底抽巷局扇备机":
                if ("启动".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.BJYK",true);
                    insertOperLog(control);
                }else if ("停止".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.BJYT",true);
                    insertOperLog(control);
                }else if ("复位".equals(split[2])){
                    writeNodeValue(2,"煤矿.15水平10605底抽巷局扇.BJFW",true);
                    insertOperLog(control);
                }
                break;
        }
    }

    private void compressedAirControl(String control) throws Exception {
        String[] split = control.split(",");
        if ("压风机1".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(2,"煤矿.空压机.KYJYQD_1",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(2,"煤矿.空压机.KYJYTZ_1",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.空压机.KYJFW_1",true);
                    insertOperLog(control);
                    break;
            }
        }else if("压风机2".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(2,"煤矿.空压机.KYJYQD_2",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(2,"煤矿.空压机.KYJYTZ_2",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.空压机.KYJFW_2",true);
                    insertOperLog(control);
                    break;
            }
        }else if("压风机3".equals(split[1])){
            switch (split[2]){
                case "一键启动":
                    writeNodeValue(2,"煤矿.空压机.KYJYQD_3",true);
                    insertOperLog(control);
                    break;
                case "一键停止":
                    writeNodeValue(2,"煤矿.空压机.KYJYTZ_3",true);
                    insertOperLog(control);
                    break;
                case "复位":
                    writeNodeValue(2,"煤矿.空压机.KYJFW_3",true);
                    insertOperLog(control);
                    break;
            }
        }
    }

    private void tunnellingControl(String control) throws Exception {
        String[] split = control.split(",");
        if ("主通风机1号风机".equals(split[1])){
            switch (split[2]){
                case "阀门开":
                    writeNodeValue(2,"煤矿.主通风机1号风机.DFYK_2",true);
                    insertOperLog(control);
                    break;
                case "阀门关":
                    writeNodeValue(2,"煤矿.主通风机1号风机.DFYG_2",true);
                    insertOperLog(control);
                    break;
                case "阀门复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.DFFW_2",true);
                    insertOperLog(control);
                    break;
                case "阀门停":
                    writeNodeValue(2,"煤矿.主通风机1号风机.DFYT_2",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYK_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYT_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYK_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYT_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQFW_4",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYK_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYK_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQYT_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.BPQFW_4",true);
                    insertOperLog(control);
                    break;
                case "联动正转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJJKZZQD_2",true);
                    insertOperLog(control);
                    break;
                case "联动正转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJJKTZ_2",true);
                    insertOperLog(control);
                    break;
                case "联动正转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJZFW_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转启动":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJJKFZQD_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转停止":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJJKTZ_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转复位":
                    writeNodeValue(2,"煤矿.主通风机1号风机.FJZFW_2",true);
                    insertOperLog(control);
                    break;
            }
        }else {
            switch (split[2]){
                case "阀门开":
                    writeNodeValue(2,"煤矿.主通风机2号风机.DFYK_2",true);
                    insertOperLog(control);
                    break;
                case "阀门关":
                    writeNodeValue(2,"煤矿.主通风机2号风机.DFYG_2",true);
                    insertOperLog(control);
                    break;
                case "阀门复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.DFFW_2",true);
                    insertOperLog(control);
                    break;
                case "阀门停":
                    writeNodeValue(2,"煤矿.主通风机2号风机.DFYT_2",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYK_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYT_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机正转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYK_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYT_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机正转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQFW_4",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYK_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "一级风机反转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQFW_3",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYK_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQYT_4",true);
                    insertOperLog(control);
                    break;
                case "二级风机反转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.BPQFW_4",true);
                    insertOperLog(control);
                    break;
                case "联动正转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJJKZZQD_2",true);
                    insertOperLog(control);
                    break;
                case "联动正转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJJKTZ_2",true);
                    insertOperLog(control);
                    break;
                case "联动正转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJZFW_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转启动":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJJKFZQD_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转停止":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJJKTZ_2",true);
                    insertOperLog(control);
                    break;
                case "联动反转复位":
                    writeNodeValue(2,"煤矿.主通风机2号风机.FJZFW_2",true);
                    insertOperLog(control);
                    break;
            }
        }
    }

    public static OpcUaClient createClientNewEndpoint(String endPointUrl, String username, String password) {
        System.out.println(endPointUrl);
        IdentityProvider identityProvider = new AnonymousProvider();
        if (!StringUtils.isEmpty(username) || !StringUtils.isEmpty(password)) {
            identityProvider = new UsernameProvider(username, password);
        }
        try {
            Function<List<EndpointDescription>, Optional<EndpointDescription>> selectEndpoint = endpoints -> {
                final Optional<EndpointDescription> endpoint = endpoints
                        .stream()
                        //SecurityPolicy.Basic256
                        .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
                        .findFirst();
                EndpointDescription newEndpoint = new EndpointDescription(endPointUrl, endpoint.get().getServer(), endpoint.get().getServerCertificate(),
                        endpoint.get().getSecurityMode(), endpoint.get().getSecurityPolicyUri(), endpoint.get().getUserIdentityTokens(),
                        endpoint.get().getTransportProfileUri(), endpoint.get().getSecurityLevel());
                return Optional.of(newEndpoint);
            };
            IdentityProvider finalIdentityProvider = identityProvider;
            OpcUaClient opcClient = OpcUaClient.create(endPointUrl,
                    selectEndpoint,
                    configBuilder -> configBuilder
                            .setApplicationName(LocalizedText.english("plc"))
                            .setApplicationUri("urn:eclipse:milo:examples:client")
                            //访问方式
                            .setIdentityProvider(finalIdentityProvider)
                            .setRequestTimeout(UInteger.valueOf(10000))
                            .build()
            );
            opcClient.connect().get();
            System.out.println("连接成功:success");
            return opcClient;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("======== opc connection fail ========");
        }
        return null;
    }
}
